const express = require('express')
const path = require('path')
const cors = require("cors");
const app = express();

// cookie和session
const session = require('express-session')
const cookieParser = require('cookie-parser')

app.use(cors()); //就这一部就已经解决了跨域

const db=require('./db/mysql')

const bodypaser=require('body-parser')
app.use(bodypaser.urlencoded({ extended: false }))
app.use(bodypaser.json())

app.use('/public',express.static(path.join(__dirname,'./upload')))
app.use(cookieParser())
// app.all("*", function(req, res, next) {
// 	// console.log(req.headers)
// 	res.set("Access-Control-Allow-Origin","");
// 	res.set("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
// 	res.set("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
// 	res.header('Access-Control-Allow-Credentials', 'true');
// 	next();
//   });
// seesion 整体配置
app.use(session({
	secret: 'sjfsdkjflajf', //为了安全性的考虑设置secret属性
	cookie: { maxAge: 60 * 1000 * 10  }, //设置过期时间
	resave: true, // 即使 session 没有被修改，也保存 session 值，默认为 true
	saveUninitialized: false, //无论有没有session cookie，每次请求都设置个session cookie ，默认给个标示为 connect.sid
}))

// 路由
const userRouter = require('./router/userRouter')
const staffRouter = require('./router/staffRouter')
const customerRouter = require('./router/customerRouter')
const orderRouter = require('./router/orderRouter')
const dishRouter = require('./router/dishRouter');
const { request } = require('express');

app.use('/user',userRouter)
app.use('/staff',staffRouter)
app.use('/customer',customerRouter)
app.use('/order',orderRouter)
app.use('/dish',dishRouter)

// (req,res,next)=>{
//   console.log(req.session)
//   if(req.session.login){
//     next()
//   }else{
//     res.send({code:-999,msg:'未登陆'})
//   }
// },

app.listen(8899,()=>{
  console.log('server start')
})